package com.SFA.ProductShow;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.FrameWork.Common.AbstractBaseActivity;
import com.FrameWork.Common.BuilderView;
import com.FrameWork.Entity.CustomerInfoEntity;
import com.FrameWork.Entity.DataEntity;
import com.FrameWork.Entity.IPageChange;
import com.FrameWork.Entity.ImagesEntity;
import com.FrameWork.Entity.ProductEntity;
import com.SFA.R;
import com.SFA.OrderManagement.AuditCPRManager;
import com.SFA.OrderManagement.ProductDetailActivity;

/*
 * 此界面通过GridView来进行产品预览，通过切换多图和单图的适配器来进行调整
 */
public class ExploreProductActivity extends AbstractBaseActivity implements
		 OnClickListener,ActionBar.TabListener  {


	/* (non-Javadoc)
	 * @显示模式切换菜单项,多图模式和单图模式,通过切换模式来进行更新
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(context);
		inflater.inflate(R.menu.explorer_product_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	List<DataEntity> brandList;
	private CustomerInfoEntity entity;
	public List<ProductEntity> list = new ArrayList<ProductEntity>();
	public List<ImagesEntity> imagesEntity = new ArrayList<ImagesEntity>();
	private List<ImageView> ivList = new ArrayList<ImageView>();
	private LinearLayout show_page,layoutForms;
	private ViewPager frag_show_img;
	public static int PHOTO_ID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectproduct_show);
		InitActivity();

	}

	public void refreshPhoto(final ProductEntity entity){
		Intent intent = new Intent(context, ProductDetailActivity.class);
		intent.putExtra("DATA", entity);
		startActivity(intent);
	
	}

	private void InitActivity(){
		layoutForms = (LinearLayout)findViewById(R.id.layoutForms);
		setTitle("产品展示");
		entity = (CustomerInfoEntity)this.getIntent().getSerializableExtra("CustomerInfo");
		ActionBar bar = getActionBar();
		brandList = new AuditCPRManager(context,null ).getDataEntityList(false,2);
		for (DataEntity entity : brandList) {
			Tab tab = bar.newTab().setText(entity.NAME).setTabListener(this);
			tab.setTag(entity);
			bar.addTab(tab);
		}
		
	}


	@Override
	protected void onStart(){
		if(frag_show_img != null)
			frag_show_img.setCurrentItem(PHOTO_ID);
		super.onStart();
	}
	@Override
	protected void onRestart() {
		if(frag_show_img != null)
			frag_show_img.setCurrentItem(PHOTO_ID);
		super.onResume();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		System.out.println(item.getItemId());
		//设置他要绑定的数据源
		return true;
	}

	@Override
	public void onClick(View view) {
		//选中一个系列的时候触发
		if(view instanceof  BuilderView )
		{
			for (int i = 0; i < layoutForms.getChildCount(); i++) {
				if (layoutForms.getChildAt(i) == view) {
					view.setSelected(true);
					//刷新产品列表
					ProductGridFragment frag = (ProductGridFragment) getFragmentManager()
							.findFragmentById(R.id.frag_show_content);
					DataEntity  data= (DataEntity) view.getTag();
					AuditCPRManager manager =  new AuditCPRManager(this,entity);
					List<ProductEntity> list =manager.getDataEntityByForm(data.CODE);
					//对过滤出来的数据做重新清理
					frag.ShowProduct(list);
				}
				else
				{
					layoutForms.getChildAt(i).setSelected(false);
				}
			}
		}
	}
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		//		System.out.println(tab.getText());
	}
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		//显示系列选项卡
		List<DataEntity> formList = new AuditCPRManager(this,null).getFormList(((DataEntity)tab.getTag()).CODE);

		layoutForms.removeAllViews();
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		params.weight=1;
		for (int i = 0; i < formList.size(); i++)
		{
			BuilderView view = new BuilderView(this);
			view.setTitle(formList.get(i).NAME);
			view.setTag(formList.get(i));
			view.setOnClickListener(this);
			layoutForms.addView(view, params);
		}
		if(formList.size()>0)
		{
			this.onClick(layoutForms.getChildAt(0));
		}

	}
	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}
	
}
